local skel = fk.CreateSkill {
  name = "emo__miaoyin",
  max_turn_use_time = 1,
}

Fk:loadTranslationTable{
  ["emo__miaoyin"] = "妙音",
  [":emo__miaoyin"] = "每回合限一次，当一张牌指定唯一目标后，你可以弃置至多两张牌令此牌额外结算X次（X为你以此法弃置牌的颜色数），且此牌结算完毕后，若你手牌数为全场最少，摸至体力上限。",

  ["#emo__miaoyin-ask"] = "噩兆：你可弃置至多2张牌，令 %arg 额外结算等于弃置牌颜色数次",
}

skel:addEffect(fk.TargetSpecified, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and data.firstTarget and data:isOnlyTarget(data.to) and not player:isNude() then
      return player:usedSkillTimes(skel.name) == 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    local cards = player.room:askToDiscard(player, {
      min_num = 1, max_num = 2, include_equip = true, cancelable = true, skill_name = skel.name, skip = true,
      prompt = "#emo__miaoyin-ask:::"..data.card:toLogString()
    })
    if #cards > 0 then
      event:setCostData(self, { cards = cards })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    if not cards then return end
    local colors = {}
    for _, id in ipairs(cards) do
      table.insertIfNeed(colors, Fk:getCardById(id).color)
    end
    data.use.additionalEffect = (data.use.additionalEffect or 0) + #colors
    room:throwCard(cards, skel.name, player, player)
    data.extra_data = data.extra_data or {}
    data.extra_data[skel.name] = data.extra_data[skel.name] or {}
    table.insert(data.extra_data[skel.name], player)
  end,
})

skel:addEffect(fk.CardUseFinished, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function (self, event, target, player, data)
    if not player.dead and player:getHandcardNum() < player.maxHp and
    data.extra_data and data.extra_data[skel.name] and table.contains(data.extra_data[skel.name], player) then
      return table.every(player.room.alive_players, function (p)
        return p:getHandcardNum() >= player:getHandcardNum()
      end)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player:drawCards(player.maxHp - player:getHandcardNum(), skel.name)
  end,
})

return skel
